<template>
  <view class="jk-page-container">
    <view class="jk-page-btn last-btn" @click="onLastBtn" :class="pageIndex === 1 ? 'disabled-style' : ''">上一页</view>
    <view class="flex-center">
      <view class="jk-current-page-index">{{ pageIndex }}</view>
      <text class="jk-page-value-separator">/</text>
      <view class="jk-page-total-count">{{ Math.ceil(totalCount / pageSize) }}</view>
    </view>
    <view class="jk-page-btn nex-btn" @click="onNetBtn" :class="pageIndex >= Math.ceil(totalCount / pageSize) ? 'disabled-style' : ''">下一页</view>
  </view>
</template>

<script>
  import {throttle} from "../../utils/jk-utils";

  export default {
    name: 'JkPage',
    props: {
      pageIndex: {
        type: Number,
        default: 1
      },
      pageSize: {
        type: Number,
        default: 10
      },
      totalCount: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        showPopup: false
      };
    },
    methods: {
      onLastBtn: throttle((e, _this) => {
        if (_this.pageIndex > 1) {
          let idx = _this.pageIndex;
          _this.$emit('page', idx - 1);
        }
      }, 500),
      onNetBtn: throttle((e, _this) => {
        if (_this.pageIndex < Math.ceil(_this.totalCount / _this.pageSize)) {
          let idx = _this.pageIndex;
          _this.$emit('page', idx + 1);
        }
      }, 500)
    }
  }
</script>
<style lang="scss" scoped>
  .jk-page-container {
    width: 100%;
    height: 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    font-size: 24rpx;
    .disabled-style {
      color: #c5c8ce;
      background: #f7f7f7;
      border-color: #dcdee2;
    }
    .jk-page-value-separator {
      margin: 0 10rpx;
    }
    .jk-page-btn {
      height: 100%;
      box-sizing: border-box;
      border: solid 1px #eee;
      padding: 2rpx 14rpx;
      border-radius: 8rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .jk-current-page-index {
      color: #2d8cf0;
    }
  }
</style>
